package com.ruoyi.web.controller.module.material;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.Material;
import com.ruoyi.web.controller.common.BaseController;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.IMaterialService;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 素材库Controller
 *
 * @author ruoyi
 * @date 2021-08-13
 */
@Validated
@Api(value = "素材库控制器", tags = {"素材库管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/material")
public class MaterialController extends BaseController {

    private final IMaterialService materialService;

    @Value("${ruoyi.imgUrl}")
	private String imgUrl;

//	@Value("${ruoyi.imgUrlOss}")
//	private String imgUrlOss;

    /**
     * 查询素材库列表
     */
    @ApiOperation("查询素材库列表")
    @GetMapping("/list")
    public TableDataInfo<Material> list(@Validated Material material) {
        QueryWrapper<Material> qw = new QueryWrapper<>();
        if(material.getGroupId()!=null){
            qw.eq("group_id",material.getGroupId());
        }
        qw.orderByDesc("create_time");
		TableDataInfo<Material> td = PageUtils.buildDataInfo(materialService.page(PageUtils.buildPage(),qw));
		List<Material> list = td.getRows();
		if(CollUtil.isNotEmpty(list)){
			list.forEach(material1 -> {
				if(StrUtil.isNotEmpty(material1.getUrl()) && (!material1.getUrl().contains("https") && !material1.getUrl().contains("http"))) {
					material1.setImgUrl(imgUrl + material1.getUrl());
				}
			});
		}
        return td;
    }

    /**
     * 获取素材库详细信息
     */
    @ApiOperation("获取素材库详细信息")
    @GetMapping("/{id}")
    public AjaxResult<Material> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") String id) {
        return AjaxResult.success(materialService.getById(id));
    }

    /**
     * 新增素材库
     */
    @ApiOperation("新增素材库")
    @Log(title = "素材库", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody Material material) {
		material.setCreateId(getUser().getNickName());
		material.setCreateTime(new Date());
//		material.setId(UUID.randomUUID().toString());

		if(StrUtil.isNotEmpty(material.getUrl())){
			String[] imgs = material.getUrl().split("\\.");
			material.setFileType(imgs[1].toUpperCase());
		}
        return toAjax(materialService.save(material) ? 1 : 0);
    }

    /**
     * 修改素材库
     */
    @ApiOperation("修改素材库")
    @Log(title = "素材库", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody Material material) {
		if(StrUtil.isNotEmpty(material.getUrl())){
			String[] imgs = material.getUrl().split("\\.");
			material.setFileType(imgs[1].toUpperCase());
		}
        return toAjax(materialService.updateById(material) ? 1 : 0);
    }

    /**
     * 删除素材库
     */
    @ApiOperation("删除素材库")
    @Log(title = "素材库" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(materialService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
